package com.jcark.mvvm.net

import androidx.lifecycle.MutableLiveData
import com.jcark.arch.net.NetConfigManager
import com.jcark.arch.net.NetworkApi
import com.jcark.arch.net.BaseRequestModel
import me.hgj.mvvm.base.viewmodel.BaseViewModel
import me.hgj.mvvm.ext.request
import me.hgj.mvvm.state.ResultState
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import java.util.*

val apiService: ApiService by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
    NetworkApi.INSTANCE.getApi(ApiService::class.java)
}

data class BaseServiceBean(var sign: String = "", var serverIP: String = "", var key: String = "")
class RequestLoginRegisterViewModel : BaseViewModel() {

    var loginResult = MutableLiveData<ResultState<UserInfo>>()
    var autoLoginResult = MutableLiveData<ResultState<UserInfo>>()
    var imgResult = MutableLiveData<ResultState<String>>()
    var loginHost = ""

    /**
     * 登录
     */
    fun loginReq(mobile: String, password: String) {
        request(
            {
//                var loginRequest = LoginRequest(mobile, MD5Utils.MD5(password))
                val stringObjectHashMap = HashMap<String, Any>()
                stringObjectHashMap["mobile"] = mobile
//                stringObjectHashMap["password"] = MD5Utils.MD5(password)
                stringObjectHashMap["type"] = 1
                stringObjectHashMap["code"] = password
//                loginRequest.setData(stringObjectHashMap)
                apiService.login(
                    url = NetConfigManager.INSTANCE.setUrlNotChange("$loginHost/yapp-app/login/smsLogin"),
                    body = BaseRequestModel(stringObjectHashMap)
                ) //请求体
            }, loginResult,
            true,
            "正在登录中"
        )
    }

    var encryptionResult = MutableLiveData<ResultState<BaseServiceBean>>()
    fun loginReq() {
        val jsob = request(
            {
//                var baseRequestViewModel = RequestViewModel()
//                baseRequestViewModel.setData(HashMap())
                apiService.requestEncryptionInfo(BaseRequestModel()) //请求体
            }, encryptionResult,
            false,
            ""
        )
    }


    fun sendCode(mobile: String) {

        var job = request({
            apiService.sendCode(
                BaseRequestModel(
                    mapOf(
                        "mobile" to mobile,
                        "validCodeType" to "TPL_REGISTER"
                    )
                )
            ) //请求体
        }, {
            it?.records
        }, {

        })
        job.cancel()
    }


    fun upImg(imgFile: File) {
        val job = request(
            {//toMediaTypeOrNull
                val img = MultipartBody.Part.createFormData(
                    "file",
                    imgFile.name,
                    RequestBody.create("multipart/form-data".toMediaTypeOrNull(), imgFile)
                )
                apiService.upImg(img) //请求体
            },
            imgResult,
            true,
            "图片上传中..."

        )
    }

//    /**
//     * 自动登录
//     */
//    fun autoLoginReq() {
//        request(
//            {
//                var autoLoginRequest = BaseRequestViewModel()
//                val stringObjectHashMap = HashMap<String, Any>()
//                autoLoginRequest.setData(stringObjectHashMap)
//                apiService.autoLogin(autoLoginRequest)
//            }, autoLoginResult,
//            false,
//            ""
//        )
//    }
}

data class UserInfo(
    var avatar: String = "",
    var nickname: String = "",
    var phoneNumber: String = "",
    var token: String = "",
    var userId: Int = 0
)

class LoginRequest(var mobile: String = "", var password: String = "") : BaseRequestModel()